package com.xsh.gls.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xsh.gls.pojo.Depot;
import com.xsh.gls.pojo.page;
import com.xsh.gls.service.DepotService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/depot")
public class DepotController {
    @Autowired
    private DepotService depotService;

    @RequestMapping("/selectdepot")
    public Object select(@RequestParam(required = false)String depotname,  @RequestParam(required = false)String pageNum,
                          HttpServletRequest request, HttpServletResponse response){


        Map<String,Object> params=new HashMap<String,Object>();
        System.out.println();
        System.out.println(depotname);
        List<Depot> depotList=null;
        Integer _pageNum=1;
        if(pageNum!=null&&!pageNum.equals("")){
            _pageNum=Integer.parseInt(pageNum);
        }
        PageHelper.startPage(_pageNum,2, "D_CREATEDATE desc");

        try {
            depotList=depotService.selectByDepotName(depotname);
            System.out.println(depotList.get(0).getDname());
        } catch (Exception e) {
            e.printStackTrace();
        }
        PageInfo<Depot> page=new PageInfo<Depot>(depotList);

       /* List<Depot> list=page.getList();*/
        return page;
    }

    @RequestMapping("/add")
    public Object add(Depot depot){
        Depot depot1=null;
        depot1.setdCreatedate(new Date());
        try {
            if (depotService.add(depot)){
                return depot1;
            }else {
                return depot1;
            }
        }catch (Exception e){
            e.printStackTrace();
            System.out.println();
        }

        return null;
    }
}
